//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖保佑             永无BUG 
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  
//                  不见满街漂亮妹，哪个归得程序员？  


/* ****************************************************************************
 * Author: 671643387
 * Date: 2015/7/22
 * ***************************************************************************/


///////////////////////////////////////////////////////////////////////////////
// 远古封印

exports.get = function(player, req, resp, onHandled) {

    do {
        if (!req.args.uid) {
            resp.code = 1;
            resp.desc = "invalid args";
            break;
        }

        if (player.user.status.level < +gConfGlobal.PeriodLevelLimit) {
            resp.code = 1;
            resp.desc = "level limit";
            break;
        }

        var _uSign = gElderSign.getByUid(req.args.uid);
        resp.data.awardTime = _uSign.awardTime;
        resp.data.signs = _uSign.signs;
    } while (false);

    onHandled && onHandled();
};

exports.open = function(player, req, resp, onHandled) {
    do {

        var fuid = +req.args.fuid;
        if (isNaN(fuid)) {
            resp.code = 1; resp.desc = 'invalid args'; break;
        }

        if (player.user.status.level < +gConfGlobal.PeriodLevelLimit) {
            resp.code = 1;
            resp.desc = "level limit";
            break;
        }

        if ((req.uid != fuid) && !gFriend.isFriend(req.uid, fuid)) {
            resp.code = 1; resp.desc = 'you are not friend'; break;
        }

        var star = getStar();

        var _openRes = gElderSign.openSign(fuid,
            req.uid,
            player.user.info.un,
            player.user.status.level,
            star);

        if (_openRes.code == 1) {
            resp.code = 1;
            resp.desc = _openRes.desc;
        } else if (_openRes.code == 2){
            resp.code = 0;
            resp.desc = _openRes.desc;
            resp.data.code = 1;
        }else {
            resp.data.star = star;  // 星数

            // 如果不是开启自己的并且好友点数没有达到每日上限，则获取奖励
            if (_openRes.slot != 0 && !player.isFriendPointFull()) {
                var uLv = player.user.status.level;
                var award;
                for (var Id in gConfEdlerSignAssistorAwards) {
                    award = gConfEdlerSignAssistorAwards[Id];
                    if (uLv <= +award.Level) {
                        break;
                    }
                }
                var pointGet = +award.Point;                    // 获取的好友点数
                var awards = _ensureAwards(award.Awards);       // 奖励道具

                // 给协助者发放奖励
                var oldPt = player.user.status.friend_point || 0;
                resp.data.friend_point = player.addFriendPoint(pointGet) - oldPt;
                resp.data.awards = player.addAwards(awards);
            }

            resp.data.slot = _openRes.slot;
            resp.data.signs = _openRes.signs;
        }
    } while (false);

    onHandled && onHandled();

    /**
     * 获取随机星数
     * @returns {*}
     */
    function getStar() {
        var oddsSum = 0;
        var oddsMap = {};
        for (var Id in gConfEdlerSignStar) {
            oddsSum += +gConfEdlerSignStar[Id]["Odds"];
            oddsMap[oddsSum] = gConfEdlerSignStar[Id];
        }
        var randVal = Math.floor(Math.random() * oddsSum);
        for (var odd in oddsMap) {
            if (randVal <= +odd) {
                return oddsMap[odd].Star;
            }
        }
        return 0;
    }
};

exports.award = function(player, req, resp, onHandled) {
    do {
        if (player.user.status.level < +gConfGlobal.PeriodLevelLimit) {
            resp.code = 1;
            resp.desc = "level limit";
            break;
        }

        var _uSign = gElderSign.getByUid(req.uid);
        if (_uSign.awardTime != 0) {
            resp.code = 1; resp.desc = 'already get'; break;
        }

        var _signs = _uSign.signs;
        if (_signs[0].uid && _signs[2].uid)
        {
            // 计算总星数
            var stars = 0;
            for (var signIdx=0; signIdx < 3; ++signIdx) {
                stars += +_signs[signIdx].star;
            }

            var awards;
            for (var Id in gConfEdlerSignOwnerAwards) {
                if (stars <= gConfEdlerSignOwnerAwards[Id].Star) {
                    awards = gConfEdlerSignOwnerAwards[Id].Awards;
                    break;
                }
            }

            // 领取奖励
            awards = _ensureAwards(awards);
            resp.data.awards = player.addAwards(awards);

            // 更新领奖时间
            _uSign.awardTime = Date.getStamp();
            gElderSign.markDirty(req.uid, _uSign);

            // 返回领奖时间
            resp.data.awardTime = _uSign.awardTime;
        } else {
            resp.code = 1;
            resp.desc = "signs not all opened";
        }

    } while (false);

    onHandled && onHandled();
};

function _ensureAwards(awards) {
    awards = JSON.parse(JSON.stringify(awards));
    if (awards instanceof Array) {
        var child = awards[0];
        if (child instanceof Array) {
            return awards;
        }
        return [awards];
    }
    return [];
}